.SUFFIXES: .cu .cu_dbg.o .c_dbg.o .cpp_dbg.o .cu_rel.o .c_rel.o .cpp_rel.o .cubin .ptx
.PRECIOUS: %.o

CUDA_INSTALL_PATH ?= /usr/local/cuda

ifdef cuda-install
	CUDA_INSTALL_PATH := $(cuda-install)
endif

# detect OS
OSUPPER   = $(shell uname -s 2>/dev/null | tr [:lower:] [:upper:])
OSLOWER   = $(shell uname -s 2>/dev/null | tr [:upper:] [:lower:])
ifndef OS
  OSNAME := $(shell uname)
endif

# 'linux' is output for Linux system, 'darwin' for OS X
DARWIN = $(strip $(findstring DARWIN, $(OSUPPER)))
ifneq ($(DARWIN),)
   SNOWLEOPARD = $(strip $(findstring 10.6, $(shell egrep "<string>10\.6" /System/Library/CoreServices/SystemVersion.plist)))
endif

# detect 32-bit or 64-bit platform
HP_64 = $(shell uname -m | grep 64)
OSARCH= $(shell uname -m)

VERBOSE :=
NVCC := nvcc
NVCCFLAGS := -I . -I ..

ifneq ($(DARWIN),)
	BOOST        ?= /opt/local
	BOOST_INC    := $(BOOST)/include
	BOOST_LIBS   := $(BOOST)/lib
	BOOST_SUFFIX := -mt
	BOOST_INC_PATH := -I $(BOOST_INC)
	BOOST_LIB_PATH := -L $(BOOST_LIBS)
	NVCCFLAGS += -m64
endif

ifeq ($(dbg),1)
	NVCCFLAGS += -g -G -D_DEBUG -DDEBUG
else
	NVCCFLAGS += -O2 -use_fast_math -DNODEBUG
endif

NVCCFLAGS += $(BOOST_INC_PATH)

#NVCCFLAGS += -g -G -D_DEBUG
#NVCCFLAGS += -g -D_DEBUG --device-debug=2
#NVCCFLAGS += --compiler-options -fno-inline
# show kernels resource usage during compilation
NVCCFLAGS += --ptxas-options=-v
# global mem cached in L2 and L1, default
#NVCCFLAGS += -Xptxas -dlcm=ca
# global mem cached in L2 only
#NVCCFLAGS += -Xptxas -dlcm=cg
# limit kernel register usage
#NVCCFLAGS += -maxrregcount X
#NVCCFLAGS += -ftz=true
# keep temporary compilation files
#NVCCFLAGS += --keep 
#SMVERSIONFLAGS := -arch=sm_20
#SMVERSIONFLAGS := -arch=sm_13
#SMVERSIONFLAGS := -arch=sm_12
#SMVERSIONFLAGS := -gencode arch=compute_13,code=sm_13
#SMVERSIONFLAGS := -gencode arch=compute_20,code=sm_20
#SMVERSIONFLAGS := -gencode arch=compute_20,code=sm_20 \
		  -gencode arch=compute_13,code=sm_13

ifneq ($(arch),)
	SMVERSIONFLAGS = -arch=sm_$(arch)
else
	SMVERSIONFLAGS = -gencode arch=compute_20,code=sm_20 \
		         -gencode arch=compute_13,code=sm_13
endif

LIBS := $(BOOST_LIB_PATH) -lboost_thread$(BOOST_SUFFIX)

SRCS=test1.cu test2.cu test3.cu test5.cu queue_mark.cu barrier_mark.cu barrier_time.cu double_queue.cu
PRGS=$(SRCS:%.cu=%)

all: $(PRGS)

test: run_test3

run_test1: test1
	./test1 -e 1000 2>&1 | tee test1.log

run_test3: test3
	./test3 -q 128 -e 100000 2>&1 | tee test3.log

run_barrier_mark: barrier_mark
	echo "WARNING: please kill this test if it deadlocks..."; \
	sleep 1; \
	./barrier_mark -c 10000 -s 2>&1 |tee /tmp/barrier_mark.dat ; \
	[ -e ../docs/barrier_mark/barrier_mark.dat ] && \
	mv -f ../docs/barrier_mark/barrier_mark.dat ../docs/barrier_mark/barrier_mark.dat.orig; \
	mv /tmp/barrier_mark.dat ../docs/barrier_mark/barrier_mark.dat

run_queue_mark: queue_mark
	./queue_mark -q 128 -e 1000 -s 2>&1 |tee /tmp/queue_mark.dat; \
	[ -e ../docs/queue_mark/queue_mark.dat ] && \
	mv -f ../docs/queue_mark/queue_mark.dat ../docs/queue_mark/queue_mark.dat.orig; \
	mv /tmp/queue_mark.dat ../docs/queue_mark/queue_mark.dat

#%: %.cu
#	$(VERBOSE)$(NVCC) $(NVCCFLAGS) $(SMVERSIONFLAGS) -o $@ $<

%: %.o
	$(VERBOSE)$(NVCC) $(NVCCFLAGS) $(SMVERSIONFLAGS) $(LIBS) -o $@ $<

%.o: %.cu
	$(VERBOSE)$(NVCC) $(NVCCFLAGS) $(SMVERSIONFLAGS) -o $@ -c $<

clean:
	rm -f $(PRGS) *.{hash,ptx,cubin,cudafe*} *.fatbin.* *.o *.i *.ii *.cu.cpp .depend

.depend: $(SRCS)
	$(VERBOSE)$(NVCC) $(NVCCFLAGS) $(SMVERSIONFLAGS) $(CPPFLAGS) -M $(SRCS) > .depend

#ifeq (.depend, $(wildcard .depend))
-include .depend
#endif
